package com.sunday.fangdaifu.adapter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.PixUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.activity.main.MainActivity;
import com.sunday.fangdaifu.activity.main.TaskFragment;
import com.sunday.fangdaifu.model.TaskCount;
import com.sunday.fangdaifu.widgets.ArcProgress;
import com.sunday.fangdaifu.widgets.OnTextCenter;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.fragment;
import static android.R.attr.textSize;
import static com.sunday.fangdaifu.R.id.view;


/**
 * Created by 刘涛 on 2017/6/30.
 */

public class CardViewAdapter extends PagerAdapter {

    private Context mContext;
    private List<View> mViewList=new ArrayList<>();
    private List<TaskCount> mData=new ArrayList<>();


    public CardViewAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<TaskCount> data) {
        mData = data;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view=null;
        if (mViewList.isEmpty()){
            view = LayoutInflater.from(container.getContext()).inflate(R.layout.layout_card_view_item, container, false);
        }else {
            view=mViewList.remove(0);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cardItemClickListener != null) {
                    cardItemClickListener.onClick(position);
                }
            }
        });
        container.addView(view);
        TaskCount item = mData.get(position);
        bind(item, view);
        return view;
    }

    private void bind(final TaskCount item, View view) {
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.card_layout);
        layout.setBackgroundResource(item.getType()==1?R.mipmap.mission_regular_bg:item.getType()==2?
                R.mipmap.mission_special_bg:R.mipmap.mission_feedback_bg);

        ArcProgress progressBar= (ArcProgress) view.findViewById(R.id.progress_bar);
        switch (item.getType()){
            case 1:
                progressBar.setUnProgressColor(mContext.getResources().getColor(R.color._1c91ff));
                progressBar.setProgressColor(mContext.getResources().getColor(R.color._70bcff));
                break;
            case 2:
                progressBar.setUnProgressColor(mContext.getResources().getColor(R.color._fa3719));
                progressBar.setProgressColor(mContext.getResources().getColor(R.color._f97661));
                break;
            case 3:
                progressBar.setUnProgressColor(mContext.getResources().getColor(R.color._3cb021));
                progressBar.setProgressColor(mContext.getResources().getColor(R.color._83d471));
                break;
        }
        if (item.getTotal()!=0){
            progressBar.setProgress(item.getFinish()/item.getTotal()*100);
        }else {
            progressBar.setProgress(0);
        }
        progressBar.setOnCenterDraw(new ArcProgress.OnCenterDraw() {
            @Override
            public void draw(Canvas canvas, RectF rectF, float x, float y, float storkeWidth, int progress) {
                Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                textPaint.setStrokeWidth(35);
                textPaint.setTextSize(PixUtils.sp2px(mContext,20));
                textPaint.setColor(ContextCompat.getColor(mContext,R.color.white));
                String progressStr = String.valueOf(item.getFinish());
                float textX = x-(textPaint.measureText(progressStr)/2);
                float textY = y-((textPaint.descent()+textPaint.ascent())/2)-PixUtils.dip2px(mContext,10);
                canvas.drawText(progressStr,textX,textY,textPaint);
                textPaint.setStrokeWidth(25);
                textPaint.setTextSize(PixUtils.sp2px(mContext,13));
                textPaint.setColor(ContextCompat.getColor(mContext,R.color._7ffffffff));
                String extraStr="已完成";
                float textX1= x-(textPaint.measureText(extraStr)/2);
                float textY1= y-((textPaint.descent()+textPaint.ascent())/2)+PixUtils.dip2px(mContext,10);
                canvas.drawText(extraStr,textX1,textY1,textPaint);
            }
        });

        TextView taskType= (TextView) view.findViewById(R.id.task_type);
        taskType.setText(item.getType()==1?"常规巡检":item.getType()==2?"分配巡检":"居民反馈");

        TextView taskTime= (TextView) view.findViewById(R.id.task_time);
        taskTime.setText(!TextUtils.isEmpty(item.getDoTime())?"执行时间"+item.getDoTime():"");
        TextView taskNum= (TextView) view.findViewById(R.id.task_num);
        taskNum.setText("("+item.getTotal()+")");
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view= (View) object;
        ((ViewPager)container).removeView(view);
        mViewList.add(view);
    }

//    //private int mChildCount=0;
//
//    @Override
//    public int getItemPosition(Object object) {
////       if (mChildCount>0){
////           mChildCount--;
////           return POSITION_NONE;
////       }
////       return super.getItemPosition(object);
//        return POSITION_NONE;
//    }

    @Override
    public void notifyDataSetChanged() {
        //mChildCount=getCount();
        super.notifyDataSetChanged();
    }

    private OnCardItemClickListener cardItemClickListener;

    public interface OnCardItemClickListener {
        void onClick(int position);
    }

    public void setOnCardItemClickListener(OnCardItemClickListener cardItemClickListener) {
        this.cardItemClickListener = cardItemClickListener;
    }

}
